package com.hkts.naturalgas.performance.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.performance.entity.*;
import com.hkts.naturalgas.performance.mapper.*;
import com.hkts.naturalgas.performance.performanceVo.FormAndItemResultVo;
import com.hkts.naturalgas.performance.performanceVo.FormAndItemsVo;
import com.hkts.naturalgas.performance.performanceVo.FormItemSortVo;
import com.hkts.naturalgas.performance.service.IHkAssessmentFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2020-12-16
 */
@Service
@Transactional
public class HkAssessmentFormServiceImpl extends ServiceImpl<HkAssessmentFormMapper, HkAssessmentForm> implements IHkAssessmentFormService {


    @Autowired
    private HkAssessmentFormMapper hkAssessmentFormMapper;
    @Autowired
    private HkAssessmentInfoMapper hkAssessmentInfoMapper;
    @Autowired
    private HkAssessmentScoreMapper hkAssessmentScoreMapper;
    @Autowired
    private HkFormEntityMapper hkFormEntityMapper;
    @Autowired
    private HkFormRelationShipMapper hkFormRelationShipMapper;

    @Override
    public IPage<HkAssessmentForm> selectPage(Page<HkAssessmentForm> page, HkAssessmentForm hkAssessmentForm) {
        QueryWrapper<HkAssessmentForm> query = Wrappers.query();
        QueryWrapper<HkAssessmentForm> entityWrapper = getEntityWrapper(query, hkAssessmentForm);
        return hkAssessmentFormMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HkAssessmentForm> selectList(HkAssessmentForm hkAssessmentForm) {
        QueryWrapper<HkAssessmentForm> query = Wrappers.query();
        QueryWrapper<HkAssessmentForm> entityWrapper = getEntityWrapper(query, hkAssessmentForm);
        return hkAssessmentFormMapper.selectList(entityWrapper);
    }
    /*
    * 根据流程实例id查询审核表单
    * */

    @Override
    public List<FormAndItemResultVo> getHkAssessmentFormByProcessId(String processId) throws Exception {
        System.out.println("在这里");
        HkAssessmentForm hkAssessmentForm =new HkAssessmentForm();
        hkAssessmentForm.setProcessInstanceId(processId);

        QueryWrapper<HkAssessmentForm> query = Wrappers.query();
        QueryWrapper<HkAssessmentForm> entityWrapper = getEntityWrapper(query, hkAssessmentForm);
        entityWrapper.orderByDesc(HkAssessmentForm.ROLE_LEVEL);
        List<HkAssessmentForm> hkAssessmentForms = hkAssessmentFormMapper.selectList(entityWrapper);
        if(hkAssessmentForms.size()==0){
            throw  new Exception("没有待审核的表单，请联系管理员！");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("process_instance_id",processId);
        /*
        * 获取考核基本信息
        * */
        List<HkAssessmentInfo> hkAssessmentInfos = hkAssessmentInfoMapper.selectByMap(map);
        HkAssessmentInfo hkAssessmentInfo = hkAssessmentInfos.get(0);
        /*
         * 获取考核分数信息
         * */
        map.put("is_current_assessment","1");
        List<HkAssessmentScore> hkAssessmentScores = hkAssessmentScoreMapper.selectByMap(map);
        List<String> listAssessmentItems=new ArrayList<String>();
       for(int i=0;i<hkAssessmentScores.size();i++){
           listAssessmentItems.add(hkAssessmentScores.get(i).getIndexId());
       }

        /*
        * 获取用户关联表单
        * */
        FormAndItemsVo vo = new FormAndItemsVo();
        HkFormEntity hkFormEntity = hkFormEntityMapper.selectFormEntityByUserId(hkAssessmentScores.get(0).getHontUserId());
        if(hkFormEntity==null){
            throw  new Exception("该用户还未绑定岗位或表单，请设置后再操作！");
        }
      vo.setHkFormEntity(hkFormEntity);
        QueryWrapper<HkFormRelationShip> query1 = Wrappers.query();
        QueryWrapper<HkFormRelationShip> eq = query1.eq(HkFormRelationShip.FORM_ID, hkFormEntity.getId());
        eq.orderByAsc(HkFormRelationShip.SORT_NUMBER);
        List<HkFormRelationShip> hkFormRelationShips = hkFormRelationShipMapper.selectList(eq);
        List<String> items=new ArrayList<String>();

        for(int i=0;i<hkFormRelationShips.size();i++){
            HkFormRelationShip hkFormRelationShip = hkFormRelationShips.get(i);
            items.add(hkFormRelationShip.getItemId());
        }
        FormItemSortVo vo1=new FormItemSortVo();
        vo1.setList(items);vo1.setFormId(hkFormEntity.getId());

        List<FormAndItemResultVo> formAndItemResultVos = hkFormRelationShipMapper.selectFormAndItemResultVoByFormItemIds(vo1);
        Iterator<FormAndItemResultVo> iterator = formAndItemResultVos.iterator();
        List<FormAndItemResultVo> tempVo=new ArrayList<>();
        while(iterator.hasNext()){
            FormAndItemResultVo next = iterator.next();
            if(listAssessmentItems.contains(next.getIndexId())){
                tempVo.add(next);
                iterator.remove();
            }
        }
        tempVo.stream().forEach(formAndItemResultVo -> {
            for(int i=0;i<hkAssessmentScores.size();i++){
                if(StringUtils.isBlank(hkAssessmentScores.get(i).getIndexId())){
                    formAndItemResultVo.setIndexComment(hkAssessmentScores.get(i).getIndexComment());
                    formAndItemResultVo.setIndexScore(hkAssessmentScores.get(i).getScore());
                }else if(formAndItemResultVo.getIndexId().equals(hkAssessmentScores.get(i).getIndexId())){
                    formAndItemResultVo.setIndexComment(hkAssessmentScores.get(i).getIndexComment());
                    formAndItemResultVo.setIndexFrequency(hkAssessmentScores.get(i).getIndexFrequency());
                }
            }
        });
        tempVo.addAll(formAndItemResultVos);
        return tempVo;
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HkAssessmentForm> getEntityWrapper(QueryWrapper<HkAssessmentForm> entityWrapper, HkAssessmentForm hkAssessmentForm) {
        //条件拼接
        if (StringUtils.isNotBlank(hkAssessmentForm.getApprovalUserId())) {
            entityWrapper.like(HkAssessmentForm.APPROVAL_USER_ID, hkAssessmentForm.getApprovalUserId());
        }
        if (StringUtils.isNotBlank(hkAssessmentForm.getFormJson())) {
            entityWrapper.like(HkAssessmentForm.FORM_JSON, hkAssessmentForm.getFormJson());
        }
        if (StringUtils.isNotBlank(hkAssessmentForm.getHountUserId())) {
            entityWrapper.like(HkAssessmentForm.HOUNT_USER_ID, hkAssessmentForm.getHountUserId());
        }
        if (StringUtils.isNotBlank(hkAssessmentForm.getProcessInstanceId())) {
            entityWrapper.like(HkAssessmentForm.PROCESS_INSTANCE_ID, hkAssessmentForm.getProcessInstanceId());
        }

        return entityWrapper;
    }
}
